#include "LabelWin32.hpp"
#include <iostream>

namespace Gui
{
    LabelWin32::LabelWin32( ControlImpl *Parent         ,
                            const std::string   &Text   ,
                            const Vector2<int>  &Pos    ,
                            const Size2<int>    &Size   )
    {
        ControlWin32* CtrlWin32;

        CtrlWin32 = dynamic_cast<ControlWin32*>(Parent);

        ControlWin32::m_hWnd = CreateWindowEx
        (
            0,
            "STATIC",
            Text.c_str(),
            SS_LEFT|WS_CHILD,
            Pos.X,Pos.Y,
            Size.Width, Size.Height,
            CtrlWin32->GetHWND(),
            NULL,
            GetModuleHandle(NULL),
            NULL
        );

        if (ControlWin32::m_hWnd == NULL)
            std::cout << "ERROR!" << std::endl;
    }


    void LabelWin32::PropertySet_TextAlignment(const TextAlignment& Alignment)
    {
        DWORD Style;
        DWORD OldStyle;

        switch (Alignment)
        {
            case Left:
                Style = SS_LEFT;
                break;

            case Center:
                Style = SS_CENTER;
                break;

            case Right:
                Style = SS_RIGHT;
                break;
            default:
                std::cout << "u.u" << std::endl;

        }

        OldStyle = GetWindowLong(ControlWin32::m_hWnd, GWL_STYLE);

        if ((OldStyle&SS_LEFT) == SS_LEFT)
            OldStyle = Left;
        else if ((OldStyle&SS_RIGHT) == SS_RIGHT)
            OldStyle = Right;
        else if ((OldStyle&SS_CENTER) == SS_CENTER)
            OldStyle = Center;

        if (ControlWin32::m_hWnd && (OldStyle != Style))
        {
            HWND hParent;

            Vector2<int> Pos;
            Size2<int> Size;
            std::string Text;
            bool Visible;

            ControlWin32::RemoveControl(ControlWin32::m_hWnd );

            hParent = GetParent(ControlWin32::m_hWnd);
            Size = this->PropertyGet_Size();
            Pos = this->PropertyGet_Position();
            Text = this->PropertyGet_Text();
            Visible = this->PropertyGet_Visible();

            DestroyWindow(ControlWin32::m_hWnd );

            ControlWin32::m_hWnd = CreateWindowEx
            (
                0,
                "STATIC",
                Text.c_str(),
                Style|WS_CHILD,
                Pos.X,Pos.Y,
                Size.Width, Size.Height,
                hParent,
                NULL,
                GetModuleHandle(NULL),
                NULL
            );

            ControlWin32::AddControl(ControlWin32::m_hWnd, this);

            this->PropertySet_Visible(Visible);
        }
    }



    TextAlignment LabelWin32::PropertyGet_TextAlignment()
    {
        TextAlignment Alignment;
        DWORD Style;

        Style = GetWindowLong(ControlWin32::m_hWnd, GWL_STYLE);

        if ((Style&SS_LEFT) == SS_LEFT)
        {
            Alignment = Left;
        }
        else if ((Style&SS_RIGHT) == SS_RIGHT)
        {
            Alignment = Right;
        }
        else if ((Style&SS_CENTER) == SS_CENTER)
        {
            Alignment = Center;
        }

        return Alignment;
    }

}
